package cn.com.pagoda.ideplugin.ui;

import cn.com.pagoda.ideplugin.listener.PagodaFrameListener;
import com.intellij.openapi.actionSystem.AnActionEvent;

import javax.swing.*;
import java.awt.*;

/**
 * 主窗体管理
 *
 * @author tianmingxing <mx.tian@qq.com> on 2023-05-06
 */
public class PagodaFrame {

    private final AnActionEvent anActionEvent;

    private final PagodaFrameListener listener;
    private final JFrame frame;
    private JPanel mainPanel;
    private CardLayout cardLayout;
    private JToolBar toolBar;
    private JButton jsonEditorBtn,
            yamlPropertiesConverterPanelBtn,
            textComparisonBtn,
            encodeAndDecodeBtn,
            timeConversionBtn,
            pwdGenBtn,
            regexpBtn,
            crontabGenBtn,
            excelConversionBtn;

    public PagodaFrame(AnActionEvent anActionEvent) {
        this.anActionEvent = anActionEvent;

        listener = new PagodaFrameListener(this);
        frame = new JFrame("Pagoda Tools");
        frame.addWindowListener(listener);

        initMainPanel();
        initCardPanel();
        initBtnPanel();

        frame.setLayout(new BorderLayout());
        frame.add(toolBar, BorderLayout.NORTH);
        JSeparator separator = new JSeparator(SwingConstants.HORIZONTAL);
        frame.add(separator, BorderLayout.CENTER);
        frame.add(mainPanel, BorderLayout.SOUTH);

        frame.pack();
        frame.setLocationRelativeTo(null); // 让JFrame居中
    }

    private void initMainPanel() {
        mainPanel = new JPanel();
        mainPanel.setBorder(BorderFactory.createEmptyBorder(0, 5, 2, 5));
        cardLayout = new CardLayout();
        mainPanel.setLayout(cardLayout);
    }

    private void initCardPanel() {
        new JsonEditorPanel(anActionEvent, mainPanel);
        new YamlPropertiesConverterPanel(anActionEvent, mainPanel);
        new TextComparisonPanel(anActionEvent, mainPanel);
        new TranscoderPanel(anActionEvent, mainPanel);
        new TimeConversionPanel(anActionEvent, mainPanel);
        new PwdGenPanel(anActionEvent, mainPanel);
        new RegexpPanel(anActionEvent, mainPanel);
        new CrontabGenPanel(anActionEvent, mainPanel);
        new ExcelConversionPanel(anActionEvent, mainPanel);
    }

    private void initBtnPanel() {
        toolBar = new JToolBar();
        toolBar.setFloatable(false);
        toolBar.setLayout(new GridLayout(1,6)); //美化布局。 后续新增按钮的话这里要+1
        toolBar.setBorder(BorderFactory.createEmptyBorder());

        jsonEditorBtn = new JButton("JSON Editor");
        jsonEditorBtn.addActionListener(listener);
        toolBar.add(jsonEditorBtn);

        yamlPropertiesConverterPanelBtn = new JButton("YAML-Properties Converter");
        yamlPropertiesConverterPanelBtn.addActionListener(listener);
        toolBar.add(yamlPropertiesConverterPanelBtn);

        timeConversionBtn = new JButton("Time Conversion");
        timeConversionBtn.addActionListener(listener);
        toolBar.add(timeConversionBtn);

        excelConversionBtn = new JButton("Excel to JSON");
        excelConversionBtn.addActionListener(listener);
        toolBar.add(excelConversionBtn);

        pwdGenBtn = new JButton("Password Gen");
        pwdGenBtn.addActionListener(listener);
        toolBar.add(pwdGenBtn);

        /*textComparisonBtn = new JButton("Text Comparison");
        textComparisonBtn.addActionListener(listener);
        toolBar.add(textComparisonBtn);*/

        encodeAndDecodeBtn = new JButton("Encode & Decode");
        encodeAndDecodeBtn.addActionListener(listener);
        toolBar.add(encodeAndDecodeBtn);

        /*regexpBtn = new JButton("Regexp");
        regexpBtn.addActionListener(listener);
        toolBar.add(regexpBtn);*/

        /*crontabGenBtn = new JButton("Crontab Gen");
        crontabGenBtn.addActionListener(listener);
        toolBar.add(crontabGenBtn);*/
    }

    /**
     * 控制主窗体的可见性
     *
     * @param b true可见，false隐藏
     * @author tianmingxing <mx.tian@qq.com> on 2023/05/19
     */
    public void setVisible(boolean b) {
        frame.setVisible(b);
    }

    public JPanel getMainPanel() {
        return mainPanel;
    }

    public CardLayout getCardLayout() {
        return cardLayout;
    }

    public JButton getJsonEditorBtn() {
        return jsonEditorBtn;
    }

    public JButton getYamlPropertiesConverterPanelBtn() {
        return yamlPropertiesConverterPanelBtn;
    }

    public JButton getTextComparisonBtn() {
        return textComparisonBtn;
    }

    public JButton getEncodeAndDecodeBtn() {
        return encodeAndDecodeBtn;
    }

    public JButton getTimeConversionBtn() {
        return timeConversionBtn;
    }

    public JButton getPwdGenBtn() {
        return pwdGenBtn;
    }

    public JButton getRegexpBtn() {
        return regexpBtn;
    }

    public JButton getCrontabGenBtn() {
        return crontabGenBtn;
    }

    public JButton getExcelConversionBtn() {
        return excelConversionBtn;
    }

    public static void main(String[] args) {
        PagodaFrame frame = new PagodaFrame(null);
        frame.setVisible(true);
    }

}
